//
//  AddCaiGouGuiXiaViewController.m
//  tangshantongjiAPPFull
//
//  Created by yuntai on 2018/4/24.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "AddCaiGouGuiXiaViewController.h"
#import "UIButton+TextImage.h"
#import "NSString+Time.h"
#import <Masonry.h>
#import "CameraTakeMamanger.h"
#import "TimeSelectView.h"

@interface AddCaiGouGuiXiaViewController ()

@property (nonatomic,copy)NSString *timeStr;
@property (nonatomic, strong) UIButton *topBtn;
@end

@implementation AddCaiGouGuiXiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];
    weakself(self)
    [self setBackBtnEventHandler:^(id sender) {
        [weakself.navigationController popToViewController:[UpLoadModelManager sharedInstance].popvc animated:YES];
    }];
    
}

-(void)dealloc{
    [UpLoadModelManager sharedInstance].popvc = nil;
    [UpLoadModelManager sharedInstance].model = nil;
}
- (void)topBtnAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    weakself(self)
    __block UIButton *btn = sender;
    TimeSelectView *view = [[TimeSelectView alloc]initWithFrame:CGRectZero selBlock:^(NSString *timestr) {
        btn.selected = !btn.selected;
        weakself.timeStr = timestr;
        [UpLoadModelManager sharedInstance].model.timeStr = timestr;
        [weakself.topBtn setTitle:timestr forState:UIControlStateNormal];
    } cancle:^{
        btn.selected = !btn.selected;
    }];
    [view show];
}

- (void)bottomLeftBtnAction:(UIButton *)sender{
    /**拍照*/
    weakself(self)
    [[CameraTakeMamanger sharedInstance]imageWithCameraInController:self handler:^(UIImage *image, NSString *imagePath) {
        [weakself uploadImageActionWithImage:image];
    } cancelHandler:^{
        
    } isIM:NO];
}
- (void)bottomRightBtnAction:(UIButton *)sender{
    /**相册*/
    weakself(self)
    [[CameraTakeMamanger sharedInstance]imageWithPhotoInController:self handler:^(UIImage *image, NSString *imagePath) {
        
        [weakself uploadImageActionWithImage:image];
        
    } cancelHandler:^{
        
    }];
}


- (void)uploadImageActionWithImage:(UIImage *)image{
    NSInteger index = 0;
    if ([UpLoadModelManager sharedInstance].model.imageurl1 == nil) {
        index = 0;
    }else if ([UpLoadModelManager sharedInstance].model.imageurl2 == nil){
        index = 1;
    }else if ([UpLoadModelManager sharedInstance].model.imageurl3 == nil){
        index = 2;
    }else if ([UpLoadModelManager sharedInstance].model.imageurl4 == nil){
        index = 3;
    }else if ([UpLoadModelManager sharedInstance].model.imageurl5 == nil){
        index = 4;
    }else if ([UpLoadModelManager sharedInstance].model.imageurl6 == nil){
        index = 5;
    }else if ([UpLoadModelManager sharedInstance].model.imageurl7 == nil){
        index = 6;
    }else if ([UpLoadModelManager sharedInstance].model.imageurl8 == nil){
        index = 7;
    }else if ([UpLoadModelManager sharedInstance].model.imageurl9 == nil){
        index = 8;
    }else if ([UpLoadModelManager sharedInstance].model.imageurl10 == nil){
        index = 9;
    }else if ([UpLoadModelManager sharedInstance].model.imageurl11 == nil){
        index = 10;
    }else if ([UpLoadModelManager sharedInstance].model.imageurl12 == nil){
        index = 11;
    }
    weakself(self)
    [HUDManager showLoadingHUDView:self.view];
    [NetWork PostUpLoadImageWithImages:image index:index pid:@"" successBlock:^(NSString *imagePath) {
        [HUDManager hideHUDView];
        UpLoadModelManager *manager = [UpLoadModelManager sharedInstance];
        [manager.model setValue:imagePath forKey:[NSString stringWithFormat:@"imageurl%ld",index+1]];
        if (index == 0) {
            AddCaiGouGuiXiaViewController *vc = [[AddCaiGouGuiXiaViewController alloc]init];
            vc.type = weakself.type;
            [weakself.navigationController pushViewController:vc animated:YES];
        }else if (index == 1){
            if (weakself.type == FullCaiGouJingLi) {
                /**采购经理 两张照片 返回了*/
                [weakself saveAction];
            }else{
                /**弹出选择框  是否继续*/
                [AlertViewManager showAlertViewMessage:@"是否要上传其他凭证照片?" handlerBlock:^{
                    
                    AddCaiGouGuiXiaViewController *vc = [[AddCaiGouGuiXiaViewController alloc]init];
                    vc.type = weakself.type;
                    [weakself.navigationController pushViewController:vc animated:YES];
                    
                } cancleBlock:^{
                    /**规下工业 返回了*/
                    [weakself saveAction];
                }];
            }
        }else{
            /**弹出选择框  是否继续*/
            [AlertViewManager showAlertViewMessage:@"是否要上传其他凭证照片?" handlerBlock:^{
                
                AddCaiGouGuiXiaViewController *vc = [[AddCaiGouGuiXiaViewController alloc]init];
                vc.type = weakself.type;
                [weakself.navigationController pushViewController:vc animated:YES];
                
            } cancleBlock:^{
                /**规下工业 返回了*/
                [weakself saveAction];
            }];
            
        }
        
        
    } FailureBlock:^(NSString *msg) {
        [HUDManager hideHUDView];
        [HUDManager showWarningWithText:@"上传失败请重试"];
        
    } errorBlock:^(id error) {
        [HUDManager hideHUDView];
        [HUDManager showWarningWithText:@"上传失败请重试"];
    }];
}

- (void)saveAction{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    
    NSString *subUrl = nil;
    if(self.type == FullGuiXiaGongYe){
        subUrl = @"api/gxcp/save.ajax";
        [params setValue:kTokenID forKey:@"token"];
        [params setValue:[UpLoadModelManager sharedInstance].model.imageurl1?:@"" forKey:@"img1"];
        [params setValue:[UpLoadModelManager sharedInstance].model.imageurl2?:@"" forKey:@"img2"];
        
        NSString *tempImg3 = nil;
        if ([UpLoadModelManager sharedInstance].model.imageurl3) {
            tempImg3 = [UpLoadModelManager sharedInstance].model.imageurl3;
            [params setValue:tempImg3?:@"" forKey:@"img3"];
        }
        if ([UpLoadModelManager sharedInstance].model.imageurl4) {
            tempImg3 = [NSString stringWithFormat:@"%@,%@",tempImg3,[UpLoadModelManager sharedInstance].model.imageurl4];
            [params setValue:tempImg3?:@"" forKey:@"img3"];
        }
        if ([UpLoadModelManager sharedInstance].model.imageurl5) {
            tempImg3 = [NSString stringWithFormat:@"%@,%@",tempImg3,[UpLoadModelManager sharedInstance].model.imageurl5];
            [params setValue:tempImg3?:@"" forKey:@"img3"];
        }
        if ([UpLoadModelManager sharedInstance].model.imageurl6) {
            tempImg3 = [NSString stringWithFormat:@"%@,%@",tempImg3,[UpLoadModelManager sharedInstance].model.imageurl6];
            [params setValue:tempImg3?:@"" forKey:@"img3"];
        }
        if ([UpLoadModelManager sharedInstance].model.imageurl7) {
            tempImg3 = [NSString stringWithFormat:@"%@,%@",tempImg3,[UpLoadModelManager sharedInstance].model.imageurl7];
            [params setValue:tempImg3?:@"" forKey:@"img3"];
        }
        if ([UpLoadModelManager sharedInstance].model.imageurl8) {
            tempImg3 = [NSString stringWithFormat:@"%@,%@",tempImg3,[UpLoadModelManager sharedInstance].model.imageurl8];
            [params setValue:tempImg3?:@"" forKey:@"img3"];
        }
        if ([UpLoadModelManager sharedInstance].model.imageurl9) {
            tempImg3 = [NSString stringWithFormat:@"%@,%@",tempImg3,[UpLoadModelManager sharedInstance].model.imageurl9];
            [params setValue:tempImg3?:@"" forKey:@"img3"];
        }
        if ([UpLoadModelManager sharedInstance].model.imageurl10) {
            tempImg3 = [NSString stringWithFormat:@"%@,%@",tempImg3,[UpLoadModelManager sharedInstance].model.imageurl10];
            [params setValue:tempImg3?:@"" forKey:@"img3"];
        }
        if ([UpLoadModelManager sharedInstance].model.imageurl11) {
            tempImg3 = [NSString stringWithFormat:@"%@,%@",tempImg3,[UpLoadModelManager sharedInstance].model.imageurl11];
            [params setValue:tempImg3?:@"" forKey:@"img3"];
        }
        if ([UpLoadModelManager sharedInstance].model.imageurl12) {
            tempImg3 = [NSString stringWithFormat:@"%@,%@",tempImg3,[UpLoadModelManager sharedInstance].model.imageurl12];
            [params setValue:tempImg3?:@"" forKey:@"img3"];
        }

    }else{
        subUrl = @"api/cgjl/save.ajax";
        [params setValue:kTokenID forKey:@"token"];
        [params setValue:[UpLoadModelManager sharedInstance].model.imageurl1?:@"" forKey:@"img1"];
        [params setValue:[UpLoadModelManager sharedInstance].model.imageurl2?:@"" forKey:@"img2"];
    }
    NSString *timesss = [[UpLoadModelManager sharedInstance].model.timeStr stringByReplacingOccurrencesOfString:@"年" withString:@"-"];
    timesss = [timesss stringByReplacingOccurrencesOfString:@"月份" withString:@""];

    [params setValue:timesss?:@"" forKey:@"time"];
    [HUDManager showLoadingHUDView:self.view];
    [NetWork PostNetWorkWithUrl:subUrl with:params successBlock:^(NSDictionary *dic) {
        
        [HUDManager showWarningWithText:@"发布成功"];
        [self.navigationController popToViewController:[UpLoadModelManager sharedInstance].popvc animated:YES];
        
    } FailureBlock:^(NSString *msg) {
        [HUDManager showWarningWithText:msg];
    } errorBlock:^(id error) {
        
    }];
    
}

- (void)initUI{
    UIButton *topbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, kNavigationHeight, kScreenBoundWidth, 44)];
    topbtn.backgroundColor = KSepLineColor;
    
    NSString *nowtimeStr = [NSString getNowTime];
    [topbtn setTitle:nowtimeStr forState:UIControlStateNormal];
    [topbtn setTitleColor:kTextDeepDarkColor forState:UIControlStateNormal];
    [topbtn setImage:[UIImage imageNamed:@"category_downTap"] forState:UIControlStateNormal];
    [topbtn setImage:[UIImage imageNamed:@"category_upTap"] forState:UIControlStateSelected];
    [topbtn leftTextRightImage];
    [topbtn addTarget:self action:@selector(topBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.topBtn = topbtn;
    [self.view addSubview:topbtn];
    
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, kScreenBoundHeight/2-20, kScreenBoundWidth, 40)];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.textColor = kTextDeepDarkColor;
    nameLabel.text = @"请上传报表照片";
    nameLabel.font = [UIFont boldSystemFontOfSize:17];
    [self.view addSubview:nameLabel];
    
    UpLoadModelManager *manager = [UpLoadModelManager sharedInstance];
    UploadImageParamsModel *model =  manager.model;
    if (model.imageurl1==nil) {
        [UpLoadModelManager sharedInstance].model = [[UploadImageParamsModel alloc]init];
        self.timeStr = nowtimeStr;
        manager.model.timeStr = self.timeStr;
        nameLabel.text = self.type == FullCaiGouJingLi ? @"请上传第一张" : @"请上传报表照片";
        self.topBtn.hidden = NO;
    }else if (model.imageurl2 == nil){
        nameLabel.text = self.type == FullCaiGouJingLi ? @"请上传第二张" : @"请上传台账照片";
        self.topBtn.hidden = YES;
    }else if (model.imageurl3 == nil){
        self.topBtn.hidden = YES;
        nameLabel.text = @"请上传凭证账照片1";
    }else if (model.imageurl4 == nil){
        self.topBtn.hidden = YES;
        nameLabel.text = @"请上传凭证账照片2";
    }else if (model.imageurl5 == nil){
        self.topBtn.hidden = YES;
        nameLabel.text = @"请上传凭证账照片3";
    }else if (model.imageurl6 == nil){
        self.topBtn.hidden = YES;
        nameLabel.text = @"请上传凭证账照片4";
    }else if (model.imageurl7 == nil){
        self.topBtn.hidden = YES;
        nameLabel.text = @"请上传凭证账照片5";
    }else if (model.imageurl8 == nil){
        self.topBtn.hidden = YES;
        nameLabel.text = @"请上传凭证账照片6";
    }else if (model.imageurl9 == nil){
        self.topBtn.hidden = YES;
        nameLabel.text = @"请上传凭证账照片7";
    }else if (model.imageurl10 == nil){
        self.topBtn.hidden = YES;
        nameLabel.text = @"请上传凭证账照片8";
    }else if (model.imageurl11 == nil){
        self.topBtn.hidden = YES;
        nameLabel.text = @"请上传凭证账照片9";
    }else if (model.imageurl12 == nil){
        self.topBtn.hidden = YES;
        nameLabel.text = @"请上传凭证账照片10";
    }
    
    
    
    UIButton *bottomLeftbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, kScreenBoundHeight-55, kScreenBoundWidth/2-2.5, 50)];
    bottomLeftbtn.backgroundColor = kNavigationColor;
    [bottomLeftbtn setTitle:@"拍照" forState:UIControlStateNormal];
    [bottomLeftbtn addTarget:self action:@selector(bottomLeftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bottomLeftbtn];
    
    UIButton *bottomRightbtn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenBoundWidth/2+2.5, kScreenBoundHeight-55, kScreenBoundWidth/2-2.5, 50)];
    bottomRightbtn.backgroundColor = kNavigationColor;
    [bottomRightbtn setTitle:@"相册" forState:UIControlStateNormal];
    [bottomRightbtn addTarget:self action:@selector(bottomRightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bottomRightbtn];
    
    [bottomLeftbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            
        } else {
            // Fallback on earlier versions
            make.left.bottom.equalTo(self.view);
            
        }
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(kScreenBoundWidth/2-2.5);
        
    }];
    [bottomRightbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(kScreenBoundWidth/2-2.5);
        if (@available(iOS 11.0, *)) {
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            
        } else {
            // Fallback on earlier versions
            make.left.bottom.equalTo(self.view);
            
        }
    }];
}
@end
